package ch.elektroplan.cmueller.logikModulGenerator.presentation.subs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.elektroplan.cmueller.logikModulGenerator.business.elements.MemoryVariable;

public class MemoryVariablePanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel lb_init;
	private JLabel lb_remanent;

	private JTextField tf_init;
	private JComboBox cb_remanent;

	private MemoryVariable memoryVariable;

	public MemoryVariablePanel(MemoryVariable memoryVariable) {

		this.memoryVariable = memoryVariable;

		lb_init = new JLabel("Initialisation Value");
		lb_remanent = new JLabel("Remanent");

		tf_init = new JTextField();

		String[] values = { "ja", "nein" };
		cb_remanent = new JComboBox(values);

		this.setPreferredSize(new Dimension(400, 50));
		this.setLayout(new GridLayout(2, 2));
		
		tf_init.addFocusListener(new LostFocusListener(tf_init));
		cb_remanent.addFocusListener(new LostFocusListener(cb_remanent));
		
		this.add(lb_init);
		this.add(lb_remanent);
		
		this.add(tf_init);
		this.add(cb_remanent);

	}

	private class LostFocusListener implements FocusListener {

		private JComponent jComponent;

		public LostFocusListener(JComponent jComponent) {
			this.jComponent = jComponent;
		}

		@Override
		public void focusGained(FocusEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void focusLost(FocusEvent e) {

			if (jComponent.equals(tf_init)) {
				JTextField tf = (JTextField) jComponent;
				memoryVariable.setInitialisationVariable(tf.getText());
			}
			if (jComponent.equals(cb_remanent)) {
				JComboBox cb = (JComboBox) jComponent;
				memoryVariable.setRemanent(cb.getSelectedIndex());
			}

		}

	}
}
